package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.FavariteMessage;
import com.shbaiche.bifenba.model.user.FollowInfoVo;
import com.shbaiche.bifenba.model.user.FollowUpdateMessage;
import com.shbaiche.bifenba.model.user.MyFollowMessage;
import com.shbaiche.bifenba.model.video.ArticleGroupVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.GanhuoFragment;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.ui.base.WebActivity;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.home.HomeActivity;
import com.shbaiche.bifenba.ui.home.HomeFragment;
import com.shbaiche.bifenba.ui.home.sub.SubInfoFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroid.event.BusFactory;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xdroidbase.router.Router;
import okhttp3.Call;


public class MyFollowActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.category_strip)
    CategoryTabStrip tabs;

    @BindView(R.id.view_pager)
    ViewPager pager;


    List<Fragment> fragmentList = new ArrayList<>();
    XFragmentAdapter adapter;

    List<FollowInfoVo> TeamList, voList;

    Dialog dg;

    FollowTeamFragment f1, f2;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void initData(Bundle savedInstanceState) {


        if (App.getPassport() == null) {
            ToastUtil.show("请先登录!");
            finish();
            return;
        }

        dg = new Dialog(this);
        dg.setContentView(R.layout.view_loading);


    }

    private void load() {
        MyFollowMessage msg = new MyFollowMessage();
        msg.setPassport(App.getPassport());
        msg.setPageSize(50);
        msg.setPageIndex(0);
        msg.setType(FollowUpdateMessage.FollowType.MATCHES);
        NetApi.MyFollow(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                MyFollowMessage msg = gson.fromJson(response.getMessage(), MyFollowMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    voList = msg.getVoList();

                    if (f1 == null) {
                        fragmentList.clear();
                        f1 = new FollowTeamFragment(TeamList, 1);
                        f2 = new FollowTeamFragment(voList, 2);
                        fragmentList.add(f1);
                        fragmentList.add(f2);

                        if (adapter == null) {
                            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, new String[]{"球队", "赛事"});
                        }

                        pager.setAdapter(adapter);
                        tabs.setViewPager(pager);
                        tabs.setAdaptive(true);
                    } else {
                        f1.setData(TeamList);
                        f2.setData(voList);
                    }
                } else {
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        dg.show();

        MyFollowMessage msg = new MyFollowMessage();
        msg.setPassport(App.getPassport());
        msg.setPageSize(50);
        msg.setPageIndex(0);
        msg.setType(FollowUpdateMessage.FollowType.TEAM);
        NetApi.MyFollow(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                Gson gson = new Gson();
                MyFollowMessage msg = gson.fromJson(response.getMessage(), MyFollowMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    TeamList = msg.getVoList();
                    load();
                } else {
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }

    @Override
    public void setListener() {

    }


    public static void launch(Activity activity) {
        Router.newIntent()
                .from(activity)
                .to(MyFollowActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_follow;
    }

    @Override
    public void setToolbar() {
        initToolbar(toolbar, "我的关注");
    }
}
